package com.suyafei.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: suyafei-1024-shop
 * @description
 * @author: luffy
 * @create: 2021-03-10 10:17
 */
public class CartVO {
    /**
     * 购物项
     */
    @JsonProperty("cart_items")
    private List<CartItemVO> cartItems;

    /**
     * 购物车总件数
     */
    @JsonProperty("total_num")
    private Integer totalNum;
    /**
     * 购物车总价格
     */
    @JsonProperty("total_price")
    private BigDecimal totalPrice;
    /**
     * 购物车时间支付价格
     */
    @JsonProperty("real_pay_price")
    private BigDecimal realPayPrice;



    public List<CartItemVO> getCartItems() {
        return cartItems;
    }

    /**
     * 总件数
     * @return
     */
    public Integer getTotalNum() {
        if (this.cartItems != null) {
            int total = cartItems.stream().mapToInt(CartItemVO::getBuyNum).sum();
            return total;
        }
        return 0;
    }

    /**
     * 总价格
     * @return
     */
    public BigDecimal getTotalPrice() {
        BigDecimal amount = new BigDecimal("0");
        if (this.cartItems != null) {
            for (CartItemVO cartItem : cartItems) {
                BigDecimal totalAmount = cartItem.getTotalAmount();
                amount = amount.add(totalAmount);
            }
        }
        return amount;
    }

    /**
     * 购物车实际支付的价格
     * @return
     */
    public BigDecimal getRealPayPrice() {
        BigDecimal amount = new BigDecimal("0");
        if (this.cartItems != null) {
            for (CartItemVO cartItem : cartItems) {
                BigDecimal totalAmount = cartItem.getTotalAmount();
                amount = amount.add(totalAmount);
            }
        }
        return amount;
    }

    public void setCartItems(List<CartItemVO> cartItems) {
        this.cartItems = cartItems;
    }

    @Override
    public String toString() {
        return "CartVO{" +
                "cartItems=" + cartItems +
                ", totalNum=" + totalNum +
                ", totalPrice=" + totalPrice +
                ", realPayPrice=" + realPayPrice +
                '}';
    }
}
